import request from '@/utils/request'
import ImageCompressor from 'image-compressor.js'



export function api_upLoad(file) {

    var fileSize = file.size;
    console.log("fileSize = " + fileSize/1024);

    const imageCompressor = new ImageCompressor();

    return new Promise((resolve, reject) => {
        imageCompressor.compress(file, {
            quality: .1,
            success(result) {
                var afterfileSize = result.size;
                console.log("afterfileSize = " + afterfileSize/1024);

                const fd = new FormData();
                // fd.append('file', result);

                fd.append('file', result, result.name);


                request({
                    url: '/upload/image',
                    method: 'post',
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    },
                    data:fd
                }).then(res => {
                    if (res && res.code === 200) {
                        resolve(res)
                    }
                }).catch((error)=>{
                    reject(error);

                })
            },
            error(e) {
                console.log(e.message);
                reject(e);
            },
        });
    });

}


// export function api_upLoad(file) {
//
//     var fileSize = file.size;
//     console.log("fileSize = " + fileSize/1024);
//
//     const fd = new FormData();
//     // fd.append('file', file);
//     fd.append('file', file, file.name);
//
//     return request({
//         url: '/upload/image',
//         method: 'post',
//         headers: {
//             'Content-Type': 'multipart/form-data'
//         },
//         data:fd
//     })
// }